package net.wanho.productManage.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.productManage.dao.OrdersDAO;
import net.wanho.productManage.po.Orders;

import java.util.List;

public class OrdersDAOImpl extends BaseDAOImpl<Orders> implements OrdersDAO {
    @Override
    public List<Orders> selectAll() throws DAOException {
        try {
            String sql = "SELECT \n" +
                    "  o.ORDER_ID orderId,\n" +
                    "  o.ORDER_SEQ orderSeq,\n" +
                    "  o.TITLE title,\n" +
                    "  o.CUSTOMER_ID \"customer.customerId\",\n" +
                    "  c.CUSTOMER_NAME \"customer.customerName\",\n" +
                    "  o.TOTAL_MONEY totalMoney,\n" +
                    "  o.STATUS \"status\",\n" +
                    "  o.OPERATOR \"operator.employeeId\",\n" +
                    "  e.EMPLOYEE_NAME \"operator.employeeName\",\n" +
                    "  o.ORDER_TIME orderTime,\n" +
                    "  o.CREATE_TIME createTime,\n" +
                    "  o.ORDER_TYPE orderType \n" +
                    "FROM\n" +
                    "  orders o \n" +
                    "  LEFT JOIN customer c \n" +
                    "    ON o.CUSTOMER_ID = c.CUSTOMER_ID \n" +
                    "  LEFT JOIN employee e \n" +
                    "    ON e.EMPLOYEE_ID = o.OPERATOR";
            Object[] params = {};
            return super.queryForList(sql,params) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean insert(Orders orders) throws DAOException {
        try {
            String sql = "INSERT INTO orders (\n" +
                    "  ORDER_ID,\n" +
                    "  ORDER_SEQ,\n" +
                    "  TITLE,\n" +
                    "  CUSTOMER_ID,\n" +
                    "  TOTAL_MONEY,\n" +
                    "  STATUS,\n" +
                    "  OPERATOR,\n" +
                    "  ORDER_TIME,\n" +
                    "  CREATE_TIME,\n" +
                    "  ORDER_TYPE\n" +
                    ") \n" +
                    "VALUES\n" +
                    "  (NULL, ?, ?, ?, ?, ?, ?, NOW(), NOW(), ?)";
            Object[] params = {orders.getOrderSeq(), orders.getTitle(), orders.getCustomer().getCustomerId(),
                    orders.getTotalMoney(),orders.getStatus(), orders.getOperator().getEmployeeId(), orders.getOrderType()};
            return super.jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public Orders selectByOrderSeq(String orderSeq) throws DAOException {
        try {
            String sql = "SELECT \n" +
                    "  ORDER_ID orderId,\n" +
                    "  ORDER_SEQ orderSeq,\n" +
                    "  TITLE title,\n" +
                    "  CUSTOMER_ID \"customer.customerId\",\n" +
                    "  TOTAL_MONEY totalMoney,\n" +
                    "  STATUS STATUS,\n" +
                    "  OPERATOR \"operator.employeeId\",\n" +
                    "  ORDER_TIME orderTime,\n" +
                    "  CREATE_TIME createTime,\n" +
                    "  ORDER_TYPE orderType \n" +
                    "FROM\n" +
                    "  orders \n" +
                    "WHERE ORDER_SEQ = ? FOR UPDATE";
            Object[] params = {orderSeq};
            return super.queryForObject(sql,params) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean update(Orders orders) throws DAOException {
        try {
            String sql = "UPDATE \n" +
                    "  orders \n" +
                    "SET\n" +
                    "  ORDER_SEQ = ?,\n" +
                    "  TITLE = ?,\n" +
                    "  CUSTOMER_ID = ?,\n" +
                    "  TOTAL_MONEY = ?,\n" +
                    "  STATUS = ?,\n" +
                    "  OPERATOR = ?,\n" +
                    "  ORDER_TIME = NOW(),\n" +
                    "  ORDER_TYPE = ? \n" +
                    "WHERE ORDER_ID = ?";
            Object[] params = {orders.getOrderSeq(), orders.getTitle(), orders.getCustomer().getCustomerId(),
                    orders.getTotalMoney(),orders.getStatus(), orders.getOperator().getEmployeeId(), orders.getOrderType(), orders.getOrderId()};
            return super.jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
}
